package com.zhou.imaotai.service.impl;


import com.zhou.imaotai.entity.SysUserEntity;
import com.zhou.imaotai.mapper.SysUserMapper;
import com.zhou.imaotai.service.SysUserService;
import com.zhou.imaotai.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lcz
 * @desc sys用户服务impl
 * @mail lcz_0130@163.com
 * @date 2024/03/26
 */

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper userMapper;

    @Override
    public SysUserEntity selectUserByUserName(String userName) {
        return userMapper.selectUserByUserName(userName);
    }

    @Override
    public SysUserEntity selectUserById(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public int updateUserProfile(SysUserEntity user) {
        return userMapper.updateById(user);
    }

    @Override
    public int resetUserPwd(Long userId, String password) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUserId(userId);
        sysUserEntity.setPassword(password);
        return userMapper.updateById(sysUserEntity);
    }

    @Override
    public boolean checkPhoneUnique(SysUserEntity user) {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUserEntity info = userMapper.checkPhoneUnique(user.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkEmailUnique(SysUserEntity user) {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysUserEntity info = userMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
            return false;
        }
        return true;
    }
}
